package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.VideoView;

import java.util.Random;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class MainActivity extends AppCompatActivity {

//    danmu
    private boolean showDanmaku;
    private DanmakuView danmakuView;
    private DanmakuContext danmakuContext;
    private Button sendButton;
    private LinearLayout sendLayout;
    private EditText editText;
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        test831
//        setContentView(R.layout.activity_main);
//        test831();

//        testdanmu
        setContentView(R.layout.danmu);
        initView();
//        playVideo();
        initDanmaku();

    }
//    test831
    private  void  test831(){
        Button button1=(Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,
                        DetailActivity.class);
                startActivity(intent);
            }
        });
    }
//    danmu
//    初始化界面控件
    private  void initView(){
        videoView=(VideoView)findViewById(R.id.videoview);
        sendLayout=(LinearLayout)findViewById(R.id.ly_send);
        sendButton=(Button)findViewById(R.id.btn_send);
        editText=(EditText)findViewById(R.id.et_text);
        danmakuView=(DanmakuView)findViewById(R.id.danmaku);
    }

//    播放视频
    private void playVideo(){
        String uri="android.resource://"+getPackageName()+"/"+R.raw.sum;
        if (uri!=null){
            videoView.setVideoURI(Uri.parse(uri));
            videoView.start();
        }else {
            videoView.getBackground().setAlpha(0);//将背景设为透明
        }
    }

    //创建弹幕解析器
    private BaseDanmakuParser parser=new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };

    //初始化弹幕
    private void initDanmaku(){
        danmakuView.setCallback(new DrawHandler.Callback() {//设置回调函数
            @Override
            public void prepared() {
                showDanmaku=true;
                danmakuView.start();//开始弹幕
                generateDanmakus();//调用随机生成弹幕方法
            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });
        danmakuContext=DanmakuContext.create();
        danmakuView.prepare(parser,danmakuContext);
    }

    private void addDanmaku(String content, boolean border){
        BaseDanmaku danmaku=danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text=content;
        danmaku.padding=6;
        danmaku.textSize=48;
        danmaku.textColor= Color.WHITE;//弹幕文字的颜色
        danmaku.setTime(danmakuView.getCurrentTime());
        if (border){
            danmaku.borderColor=Color.BLUE;//弹幕文字边框的颜色
        }
        danmakuView.addDanmaku(danmaku);//添加一条弹幕
    }

    //随机生成一些弹幕内容
    private void generateDanmakus(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (showDanmaku){
                    int num=new Random().nextInt(300);
                    String content=""+num+num;
                    addDanmaku(content,false);
                    try {
                        Thread.sleep(num);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    @Override
    protected void onPause(){
        super.onPause();
        if (danmakuView!=null&&danmakuView.isPrepared()){
            danmakuView.pause();
        }
    }
}


